package com.example.zhouzhaocheng;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.apache.commons.lang3.StringEscapeUtils;

public class MainActivity2 extends AppCompatActivity {
private String url ="https://tianqiapi.com/api?version=v1&appid=65716696&appsecret=pPQF7EgL&city=";
    private EditText cityE;
    private TextView weather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        cityE = findViewById(R.id.cityE);
        weather = findViewById(R.id.weather);
//        postByOkGo(url+cityE.getText().toString());
    }

    private void postByOkGo(String url) {
        OkGo.<String>get(url)
                .tag(this)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String newValue = StringEscapeUtils.unescapeJava(response.body());

                        WeatherBean localBean =
                                new Gson().fromJson(
                                        new Gson().toJson(newValue),
                                        new TypeToken<WeatherBean>() {
                                        }.getType()
                                );

                        weather.setText(newValue);

                    }
                });
    }
    public void search(View view) {
        postByOkGo(url+cityE.getText().toString());
    }
}